home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / AIFF DSP v22 / plugin_src / cardread.c < prev    next >
Text File  |  1995-01-30  |  3KB  |  134 lines

  1. /* displays the waveform in an AIFF file and does some analysis specific to magnetic card waveforms */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #ifdef THINK_C
  6. #include <console.h>
  7. #endif
  8.  
  9. #include "plugin_specific.h"
  10. #include "aiff.h"
  11. #include "card.h"
  12.  
  13. static FILE *tmpfil, *peakfil;
  14.  
  15. void init_process_cardread( void ) {
  16.    if (nh.wdsi != 8) err( "Cannot process a file with this word size" );
  17.    if (nh.chan != 1) err( "Cannot process a multichannel file" );
  18.    
  19.    tmpfil  = tmpfile();
  20.    peakfil = tmpfile();
  21. }
  22.  
  23. #define FLAGVAL 129
  24.  
  25. void process_samdat_cardread( long buflen ) {
  26. #define THRESH 20
  27. #define FLAG(x) ( (x<0) ? -FLAGVAL : FLAGVAL )
  28.    
  29.    short scratch, i;
  30.    char *td = d;
  31.    static long global_pos = 0;
  32.    static char findmax = 0, max = 0;
  33.    pkpt p;
  34.    
  35.    for (i=0; i<buflen; i++, global_pos++) {
  36.       scratch = *td;
  37.       if ( findmax && *td > max ) max = *td;
  38.       if ( *td > THRESH ) findmax = 1;
  39.       else
  40.       {
  41.          if ( findmax==1 )  /* if we were looking for max */
  42.          {
  43.             p.pos = global_pos;
  44.             p.val = max;
  45.             max = 0;
  46.             fwrite( &p, sizeof p, 1, peakfil );
  47.             scratch = FLAG(*td);
  48.          }
  49.          findmax = 0;
  50.       }
  51.       td++;
  52.       fwrite( &scratch, 2, 1, tmpfil );
  53.    }
  54. }
  55.  
  56. #define ROWS    23
  57. #define TOTCOLS 80
  58. #define COLS    TOTCOLS-7
  59. #define TOTROWS ROWS+1
  60.  
  61. static char s[(TOTCOLS+1)*TOTROWS+1] = "", *tmps;
  62.  
  63. void samstr( short samval )
  64. {
  65.    static char regspaces[COLS+1] = "", invspaces[COLS+1] = "";
  66.    char *spaces;
  67.    int col_i, wdsi = nh.wdsi;
  68.    
  69.    if ( !*regspaces ) memset( regspaces,        ' ', COLS );
  70.    if ( !*invspaces ) memset( invspaces, 0x80 | ' ', COLS );
  71.    
  72.    if ( samval == FLAGVAL || samval == -FLAGVAL)
  73.    {
  74.       col_i = COLS;
  75.       spaces = invspaces;
  76.    }
  77.    else
  78.    { 
  79.       col_i = (samval+(1L<<(wdsi-1))) * (COLS+1) / (1L<<wdsi);
  80.       spaces = regspaces;
  81.    }
  82.  
  83.    sprintf( tmps, "%6d%.*s|\n", samval, col_i, spaces );
  84.    tmps += col_i + 8;
  85. }
  86.  
  87. void view( void )
  88. {
  89.    char c;
  90.    short buf[ROWS], *tbuf, i, ofs = 0;
  91.  
  92. #ifdef THINK_C
  93.    csetmode( C_CBREAK, stdin );
  94.    cinverse( TRUE, stdout );
  95. #endif
  96.    fseek( tmpfil, 0, SEEK_SET );
  97.    
  98.    do
  99.    {
  100.       fseek( tmpfil, ofs << 1, SEEK_CUR );
  101.       fread( buf, ROWS << 1, 1, tmpfil );
  102. #ifdef THINK_C
  103.       cgotoxy( 1, 1, stdout );
  104.       ccleos( stdout );
  105. #endif
  106.       tmps = s;
  107.  
  108.       for (i=0, tbuf=buf; i<ROWS; i++) samstr( *tbuf++ );
  109.  
  110.       fprintf( stderr,"%sfile pos: %ld (d/u line, f/b screen, x exit) ",
  111.                s, ftell( tmpfil ) );
  112.  
  113.       ofs = -ROWS + (( (c = getchar()) == 'b' ) ? -ROWS : 
  114.                      (  c              == 'f' ) ?  ROWS :
  115.                      (  c              == 'r' ) ?  -10*ROWS :
  116.                      (  c              == 'l' ) ?   10*ROWS :
  117.                      (  c              == 'u' ) ? -1 :
  118.                      (  c              == 'd' ) ?  1 : 0 );
  119.    }
  120.    while ( c != 'x' );
  121.    
  122.    fclose( tmpfil );
  123. }
  124.  
  125. void term_process_cardread ( void )
  126. {
  127.    view();
  128.    post_process( peakfil );
  129.    fclose( peakfil );
  130. }
  131.  
  132. plugin_info plugin_cardread = { init_process_cardread,
  133.    term_process_cardread, process_samdat_cardread, 1, 0 };
  134.